package com.lujianfei.powersearch.timer

import android.util.Log
import java.util.Timer
import java.util.TimerTask

class GlobalTimerManager private constructor() {

    companion object {
        val instance by lazy { GlobalTimerManager() }
    }

    private var timer:Timer ?= null
    private val observers = arrayListOf<OnGlobalCallback>()

    fun addObserver(observer:OnGlobalCallback) {
        observers.add(observer)
    }

    fun removeObserver(observer:OnGlobalCallback) {
        observers.add(observer)
    }

    fun start() {
        if (timer != null) {
            timer?.cancel()
        }
        timer = Timer()
        timer?.schedule(object : TimerTask() {
            override fun run() {
                for (i in 0 until observers.size) {
                    observers[i].onGlobalTimerCallback()
                }
            }
        }, 1000, 1000)
    }

    fun end() {
        timer?.cancel()
    }

    interface OnGlobalCallback {
        fun onGlobalTimerCallback()
    }
}